<template>
  <div class="app-container">
    <div v-if="!detailVisible">
      <div class="search">
        <el-input v-model="form.name" style="width: 200px;margin-right: 20px" clearable placeholder="报表名称" />
        <el-select v-model="form.frequency" clearable filterable placeholder="请选择">
          <el-option
            v-for="tem in taskFrequency"
            :key="tem.value"
            :label="tem.label"
            :value="tem.value"
          />
        </el-select>
        <el-button type="primary" icon="el-icon-search" style="margin-left: 40px" @click="setResult">查询</el-button>
        <el-button type="primary" icon="el-icon-refresh" @click="reset">重置</el-button>
      </div>
      <el-table
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column prop="name" label="报表名称">
          <template slot-scope="scope">
            <div>{{ scope.row.sheet_template && scope.row.sheet_template.name }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="报表类型">
          <template slot-scope="scope">
            <div class="report-type">{{ scope.row.frequency == 'monthly' ? '月度' : '一次性' }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="更新时间">
          <template slot-scope="scope">
            <div>{{ getTimeFormate(scope.row.updated_at) }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="填报单位">
          <!-- <template slot-scope="scope">
            <div>{{ scope.row.frequency == 'monthly' ? '按月' : '一次性' }}</div>
          </template> -->
        </el-table-column>
        <el-table-column prop="name" label="任务频次">
          <template slot-scope="scope">
            <div>{{ scope.row.frequency == 'monthly' ? '按月' : '一次性' }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="统计周期">
          <template slot-scope="scope">
            <div v-if="scope.row.frequency == 'monthly'">{{ scope.row.year + '年' + scope.row.key + '月' }}</div>
            <div v-else>{{ scope.row.key }}</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="210" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="handleDetail(scope.row)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination :total="total" :limit="10" :background="false" :page="currentPage" @pageSize="handleSizeChange" @pagination="handleCurrentChange" />

    </div>
    <div>
      <Detail v-if="detailVisible" :item="item" @goBack="goBack" />
    </div>
  </div>
</template>
<script>
import moment from 'moment'
import { getCookie } from '@/api/common'
import { getAaggregateReport } from '@/api/table'
import Detail from './detail.vue'
export default {
  components: {
    Detail
  },
  data() {
    return {
      tableData: [],
      form: {
        name: '',
        type: ''
      },
      item: {},
      listLoading: false,
      detailVisible: false,
      total: 0,
      currentPage: 1,
      pageSizes: 10,
      taskFrequency: [
        {
          value: 'monthly',
          label: '按月'
        },
        {
          value: 'once',
          label: '一次性'
        }
      ]
    }
  },
  created() {
    this.fetchData()
    // this.getCookieToken()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getAaggregateReport({ ...this.form, page: this.currentPage, per_page: this.pageSizes }).then(response => {
        this.tableData = response.data
        this.total = response.pagination.total
        this.currentPage = response.pagination.current_page
        console.log(response.data, '------------------response.data')
        this.listLoading = false
      })
    },
    getCookieToken() {
      getCookie().then(response => {
      })
    },
    handleSizeChange(val) {
      this.pageSizes = val.limit
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.currentPage = val.page
      this.fetchData()
    },
    handleDetail(row) {
      const routeUrl = this.$router.resolve({
        path: '/Report/detail',
        query: {
          id: row.id
        }
      })
      window.open(routeUrl.href, '_blank')
      // this.item = row
      // this.detailVisible = true
    },
    goBack() {
      this.detailVisible = false
    },
    getTimeFormate(date) {
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },
    reset() {
      this.currentPage = 1
      this.form = {
        name: '',
        type: ''
      }
      this.fetchData()
    },
    setResult() {
      this.currentPage = 1
      this.fetchData()
    }
  }
}
</script>
<style lang="scss" scoped>
.search {
  margin-bottom: 20px;
}
.report-type {
  background-color: #51d251;
  color: #fff;
  width: 50px;
  text-align: center;
  border-radius: 5px;
  // padding: 10px 10px;
}
</style>
